﻿(function() {
    var global_target = null;

    var accordion_head_template = null;
    var accordion_content_template = null;

    function init_params() {
        if (global_options.title_height == "small")
            accordion_head_template = "<h3 class='accor_head_gather small' style='height:20px;'><span></span>[name]</h3>";
        else
            accordion_head_template = "<h3 class='accor_head_gather'><span></span>[name]</h3>";
        accordion_content_template = "<div class='accor_content_gather'>[content]</div>";
    }

    // 生成折叠插件
    function produce_accordion() {
        var heads = $(global_target).children("div").children("h3");
        var contents = $(global_target).children("div").children("div");
        var accordionCount = heads.length;
        var accordion = "";
        for (var accordionIndex = 0; accordionIndex < accordionCount; accordionIndex++) {
            accordion += "<div>";
            $(heads[accordionIndex]).children("a:eq(0)").addClass("accor_title");
            var a = $(heads[accordionIndex]).html();
            accordion += accordion_head_template.replace(/\[name\]/, a);

            accordion += accordion_content_template.replace(/\[content\]/, $(contents[accordionIndex]).html());
            accordion += "</div>";
        }
        return accordion;
    }

    // 插件头单击事件
    function head_click(tag) {
        var slide_all = global_options.slide_all
        if (!slide_all) {
            slide_single(tag);
        } else {
            var accordions = $(tag).parent().parent().parent().children();
            var spread = $(tag).parent().attr("class").indexOf('accor_head_gather') == -1;
            $.each(accordions, function(key, accordion) {
                var head = $(accordion).children("h3").children("a");
                var content = $(accordion).children("div");
                $(head).parent().removeClass("accor_head_spread");
                $(head).parent().addClass("accor_head_gather");
                if (content.css("display") == "block")
                    accor_straggle(content, "gather");

            })
            if (!spread)
                slide_single(tag);
        }
        return false;
    }

    // 头部事件触发主体展开或收拢
    function slide_single(tag) {
        var content = $(tag).parent().parent().children("div");
        if (!$(tag).parent().attr("class")) return;
        if ($(tag).parent().attr("class").indexOf("accor_head_gather") != -1) {
            $(tag).parent().removeClass("accor_head_gather");
            $(tag).parent().addClass("accor_head_spread");
            accor_straggle(content, "spread");
        } else {
            $(tag).parent().removeClass("accor_head_spread");
            $(tag).parent().addClass("accor_head_gather");
            accor_straggle(content, "gather");
        }
    }

    // 主体展开或收拢
    function accor_straggle(tag, pattern) {
        if (pattern == "gather") {
            $(tag).animate({ height: 0 }, "slow", null, function() {
                $(tag).removeClass("accor_content_spread").addClass("accor_content_gather");
                $(tag).css("display", "none");
            });
        } else if (pattern == "spread") {
            $(tag).animate({ height: 100 }, "slow", null, function() { $(tag).removeClass("accor_content_gather").addClass("accor_content_spread"); $(tag).attr("style", "display:block"); });
        }
    }

    // 绑定事件
    function binding_event() {
        var head = $(global_target).children("div").children("h3");
        for (var i = 0; i < head.length; i++) {
            var a = $(head[i]).children("a:eq(0)");
            $(a).unbind("click");
            $(a).bind("click", function() { head_click(this); return false; });
        }
    }

    // 全局配置文件
    var global_options = null;

    $.fn.selfAccordion = function(options) {
        var default_options = {
            slide_all: true,
            title_height: "small"  //normal
        };
        global_options = $.extend(default_options, options);

        global_target = $(this);

        init_params();
        $(global_target).html(produce_accordion());

        binding_event();
        var first_tag = $(global_target).children("div:eq(0)").children("h3").children("a");
        head_click(first_tag);
    };
})(jQuery);
